package com.sugon.controller.device;

import com.sugon.entity.DevTypeEntity;
import com.sugon.service.DevTypeService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 设备类型Controller
 *
 * @author sugon
 * @date 2019-07-15 09:39:14
 */
@Controller
@RequestMapping("devtype")
public class DevTypeController {
    @Autowired
    private DevTypeService devTypeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("devtype:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<DevTypeEntity> devTypeList = devTypeService.queryList(query);
        int total = devTypeService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(devTypeList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public R listAll(@RequestParam Map<String, Object> params) {
        //查询列表数据
        List<DevTypeEntity> devTypeList = devTypeService.queryList(params);
        return R.ok().put("list", devTypeList);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("devtype:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        DevTypeEntity devType = devTypeService.queryObject(id);

        return R.ok().put("devType", devType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("devtype:save")
    @ResponseBody
    public R save(@RequestBody DevTypeEntity devType) {
        // 校验设备类型是否存在
        DevTypeEntity devTypeInDB = devTypeService.queryObjectByCode(devType.getTypeCode());
        if (devTypeInDB != null) {
            return R.error("设备类型编码已存在！");
        }
        return devTypeService.save(devType) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("devtype:update")
    @ResponseBody
    public R update(@RequestBody DevTypeEntity devType) {
        devTypeService.update(devType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("devtype:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        devTypeService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        params.put("sidx", "TYPE_CODE");
        params.put("order", "asc");
        List<DevTypeEntity> list = devTypeService.queryList(params);

        return R.ok().put("list", list);
    }
}
